<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        .processImageView {
            position: absolute;
            border: 2px solid red;
            padding: 2px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="searchFrm" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">公告标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">发布人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="opername" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" id="startTime" readonly="readonly"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" id="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id" id="title_id"/>
                        <input type="text" name="title" id="title" lay-verify="required" autocomplete="off"
                               placeholder="请输入标题"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公告内容</label>
                    <div class="layui-input-block">
                        <div id="content" name="content" style="margin: 50px 0 50px 0">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label
                                class="layui-icon layui-icon-release"></label>提交</a>
                        <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataFrm')[0].reset()"
                           lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div>
        <!--添加和修改的弹出层结束-->

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn layui-btn-normal" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="batchdelete"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"> 编辑 </a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"> 删除 </a>
            <a class="layui-btn layui-btn-xs layui-bg-cyan data-count-delete" lay-event="show"> 查看 </a>
        </script>

    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    let tableIns;
    let mainIndex;//打开添加公告的弹层
    let url;  //动态的url

    layui.use(['form', 'table', 'laydate', 'layer', 'wangEditor'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            layer = layui.layer;
        wangEditor = layui.wangEditor;

        //渲染时间选择器
        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        tableIns = table.render({
            elem: '#currentTableId',
            url: api + 'notice/loadAllNotice',
            toolbar: "#toolbarDemo",
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: 'id', title: 'ID', align: "center"},
                {field: 'title', title: '公告标题', align: "center"},
                {field: 'createtime', title: '发布时间', align: "center"},
                {field: 'opername', title: '发布人', align: "center"},
                {title: '操作', templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            page: true
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let params = $("#searchFrm").serialize();
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , url: api + 'notice/loadAllNotice?' + params
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAddNotice();
                return false;
            } else // 监听删除操作
            if (obj.event === 'batchdelete') {
                let checkStatus = table.checkStatus('currentTableId');
                let data = checkStatus.data;
                let length = checkStatus.data.length;
                if (length == 0) {
                    layer.msg("请选中要删除的行数据");
                } else {
                    layer.confirm('真的删除选择中这些数据么?', function (index) {
                        var params = "";
                        $.each(data, function (index, item) {
                            if (index == 0) {
                                params += "ids=" + item.id;
                            } else {
                                params += "&ids=" + item.id;
                            }
                        });

                        $.post(api + "notice/batchDeleteNotice", params, function (res) {
                            layer.msg(res.msg);
                            tableIns.reload();
                            layer.close(index);
                        })
                    });
                }
            }
        });

        function openAddNotice() {
            mainIndex = layer.open({
                type: 1,
                title: '添加公告',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    //重置表单
                    $("#dataFrm")[0].reset();
                    editor.txt.html("");
                    url = api + "notice/addNotice";
                }
            });
        }

        //提交数据
        form.on("submit(doSubmit)", function (data) {
            let params = $("#dataFrm").serialize();
            let content = editor.txt.html();
            params += "&content=" + content;
            $.post(url, params, function (res) {
                layer.msg(res.msg);
                if (res.code == 200) {
                    tableIns.reload();
                }
                //重置表单
                $("#dataFrm")[0].reset();
                editor.txt.html("");
                layer.close(mainIndex);
            })
            return false;
        })


        //初始化富文本
        let editor = new wangEditor('#content');
        editor.customConfig.uploadImgServer = "../api/upload.json";
        editor.customConfig.uploadFileName = 'image';
        editor.customConfig.pasteFilterStyle = false;
        editor.customConfig.uploadImgMaxLength = 5;
        editor.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                if (result.code == 1) {
                    var url = result.data.url;
                    url.forEach(function (e) {
                        insertImg(e);
                    })
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor.customConfig.customAlert = function (info) {
            layer.msg(info);
        };
        editor.create();


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openUpdateNotice(data);
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除这条数据么', function (index) {
                    $.post(api + "notice/deleteNotice", {id: data.id}, function (res) {
                        layer.msg(res.msg);
                        obj.del();
                        layer.close(index);
                        tableIns.reload();
                    })
                });
            } else if (obj.event === 'show') {
                showNotice(data);
            }
        });

        //查看公告
        function showNotice(data) {
            var title = data.title;
            var content = data.content;
            var noticeTime = data.createtime;
            var opername = data.opername;
            var html = '<div style="padding:15px 20px; text-align:justify; line-height: 22px;border-bottom:1px solid #e2e2e2;background-color: #2f4056;color: #ffffff">\n' +
                '<div style="text-align: center;margin-bottom: 20px;font-weight: bold;border-bottom:1px solid #718fb5;padding-bottom: 5px"><h4 class="text-danger">' + title + '</h4></div>\n' +
                '<div style="font-size: 12px">' + content + '</div>\n' +
                '</div>\n';
            parent.layer.open({
                type: 1,
                title: '系统公告' + '<span style="float: right;right: 1px;font-size: 12px;color: #b1b3b9;margin-top: 1px">发布时间:' + noticeTime + ' 发布人:' + opername + '</span>',
                area: '800px;',
                shade: 0.8,
                btn: ['关闭'],
                btnAlign: 'c',
                moveType: 1,
                content: html
            });
        }

        function openUpdateNotice(data) {
            mainIndex = layer.open({
                type: 1,
                title: '修改[' + data.title + ']公告',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    $("#title").val(data.title);
                    $("#title_id").val(data.id);
                    url = api + "notice/updateNotice";
                    //装载数据表格
                    form.val("dataFrm", data);
                    //设置富文本的内容
                    editor.txt.html(data.content);
                }
            });
        }

    });
</script>

</body>
</html>
